package bmatch.tech.cloud.resource;

import bmatch.tech.cloud.auth.TokenNeeded;
import bmatch.tech.cloud.model.Response;
import bmatch.tech.cloud.service.DirectoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Component
@Path("directory")
public class DirectoryResource {

    /**
     * 日志输出
     */
    private static Logger log = LoggerFactory.getLogger(DirectoryResource.class);

    /**
     * 文件夹服务类
     */
    DirectoryService directoryService;

    @Autowired
    public DirectoryResource(DirectoryService directoryService) {
        this.directoryService = directoryService;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("create")
    public Response create(
            @HeaderParam("userId") String userId,
            @QueryParam("path") String path,
            @QueryParam("name") String name) {
        return directoryService.create(userId, path, name);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("delete")
    public Response delete(@HeaderParam("userId") String userId,
                           @QueryParam("path") String path,
                           @QueryParam("name") String name) {
        return directoryService.delete(userId, path, name);
    }


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("rename")
    public Response rename(@HeaderParam("userId") String userId,
                           @QueryParam("path") String path,
                           @QueryParam("originalName") String originalName,
                           @QueryParam("targetName") String targetName) {
        return directoryService.rename(userId, path, originalName, targetName);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @TokenNeeded
    @Path("shift")
    public Response shift(@HeaderParam("userId") String userId,
                          @QueryParam("name") String name,
                          @QueryParam("originalPath") String originalPath,
                          @QueryParam("targetPath") String targetPath) {
        return directoryService.shift(userId, name, originalPath, targetPath);
    }


}
